package com.example.ui.learn;

import java.io.File;
import java.util.List;
import com.example.DBHelper.Dao;
import com.example.downloadmanager.Downloader;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.ui.leftmenu.DownItem;
import com.example.ui.leftmenu.Itme10View;
import com.example.util.AsyncLoadImage;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class DownListAdapter extends BaseAdapter implements OnClickListener {

	private Context context = null;
	List<String> adlist;
	private AsyncLoadImage asyncLoadImage;
	private Dao dao;

	public DownListAdapter(Context context2, List<String> listUrl) {
		this.adlist = listUrl;
		this.context=context2;
		dao=HSApplication.getDao();
		 asyncLoadImage=new AsyncLoadImage();
	}
	@Override
	public int getCount() {
		return adlist.size();
	}
	@Override
	public Object getItem(int position) {
		return null;
	}
	@Override
	public long getItemId(int position) {
		return 0;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = null;
		if (convertView == null) {
			convertView = View.inflate(this.context, R.layout.item10_listitem,
					null);
		}
		String strUrl = adlist.get(position);
		DownItem downitem = DownUtils.listDowned.get(strUrl);
		 convertView.findViewById(R.id.btn_continue).setVisibility(4);
		 convertView.findViewById(R.id.btn_pause).setVisibility(4);
		 convertView.findViewById(R.id.progressBar).setVisibility(4);
		 convertView.findViewById(R.id.txt4).setVisibility(4);
		TextView Title = (TextView) convertView.findViewById(R.id.txt1);
		TextView Sub_title = (TextView) convertView.findViewById(R.id.txt2);
		TextView jindu = (TextView) convertView.findViewById(R.id.txt3);
		TextView Name = (TextView) convertView.findViewById(R.id.txt5);
		Button Btn_shiting=(Button)convertView.findViewById(R.id.Btn_shiting);
		Button Btn_delete=(Button) convertView.findViewById(R.id.btn_delete);
		ImageView Teacher_pic = (ImageView) convertView
				.findViewById(R.id.teacher_pic);
		Title.setText(downitem.getTitle());
		Sub_title.setText(downitem.getSub_title());
		Name.setText(downitem.getName());
		jindu.setText(downitem.getJindu().getText());
		asyncLoadImage.imageLoader.displayImage(downitem.getPic_Url(), Teacher_pic,asyncLoadImage.options);

		downitem.setBtn_shiting(Btn_shiting);
		downitem.getBtn_shiting().setOnClickListener(this);
		downitem.getBtn_shiting().setTag(strUrl);
		downitem.setBtn_shiting((Button) convertView
				.findViewById(R.id.Btn_shiting));
		downitem.setBtn_delete(Btn_delete);
		
		downitem.getBtn_delete().setOnClickListener(this);
		downitem.getBtn_delete().setTag(strUrl);
		if(!downitem.isChange){
			downitem.getBtn_shiting().setVisibility(0);
			downitem.getBtn_delete().setVisibility(4);
		}else{
			downitem.getBtn_shiting().setVisibility(4);
			downitem.getBtn_delete().setVisibility(0);
		}
		return convertView;
	}
	DownItem downitem ;
	File f;
	@Override
	public void onClick(View v) {
		int index = DownUtils.downUrl.indexOf(v.getTag());
		 downitem = DownUtils.listDowned.get(v.getTag());
		 String lisname=downitem.getUrl().substring(downitem.getUrl().lastIndexOf("/") + 1);
		 f=new File(ListeningActivity.filepath+lisname);
		switch (v.getId()) {
		case R.id.btn_delete:
			new AlertDialog.Builder(context)
			.setTitle("您的操作会删除该文件！")
			.setMessage("是否真的要删除？")
			.setNegativeButton(
							"取消",
							new android.content.DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									return;
								}
							})
			.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							DownUtils.listDowned.remove(downitem);
							DownUtils.listUrl.remove(downitem.getUrl());
							dao.deleteListen(downitem.getUrl());
							f.delete();
							h.sendEmptyMessage(0);
						}
					}).show();
			
			break;
		case R.id.Btn_shiting:
			ListeningInfo listenInfo;
			 Intent i1_l = new Intent();
				i1_l.setClass(context, ListeningActivity.class);
				listenInfo=new ListeningInfo(downitem.getUrl(),downitem.getPic_Url(),downitem.getTitle(),downitem.getName(),downitem.getTer_Id(),downitem.getTag(),downitem.getColum_Id(),"",-1,-1,0);
				Bundle lisinfo=new Bundle();
				lisinfo.putParcelable(ListeningActivity.LISTEN,listenInfo);
				i1_l.putExtras(lisinfo);
				i1_l.putExtra(ListeningActivity.ISLISTENING,false);
				i1_l.putExtra("intro",downitem.getIsCollet());
				context.startActivity(i1_l);
			break;
		}
	}
	Handler h=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				Itme10View.down_list.setAdapter(DownUtils.Downadapter);
				DownUtils.Downadapter.notifyDataSetChanged();
				break;
			case 1:
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};
}
